package com.mgetool.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ScrollBar;

import com.mgetool.ui.page.PageManager;
import com.mgetool.ui.page.PageViewInterface;
import org.eclipse.swt.graphics.Point;

public class CanvasComposite extends Composite implements Runnable{

	private boolean isAlive;
	//刷新间隔时间 毫秒
	public static int delay = 50;
	/**
	 * page管理类
	 */
	private PageManager pageManager;
	
	private Canvas canvas;
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public CanvasComposite(Composite parent, int style) {
		super(parent,  /*SWT.H_SCROLL  
				| SWT.V_SCROLL | */SWT.BORDER);
//		setExpandVertical(true);
//		setExpandHorizontal(true);
		
		setLayout(new FillLayout());
		final ScrolledComposite composite = new ScrolledComposite(this, SWT.H_SCROLL  
				| SWT.V_SCROLL );
//		composite.setLayout(new FillLayout());
		composite.setExpandVertical(true);
		composite.setExpandHorizontal(true);
//		composite.setLayout(new GridLayout(1, false));
//		setLayout(new GridLayout(1, false));
//		composite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		pageManager = new PageManager();
		canvas = new Canvas(composite, SWT.NO_BACKGROUND | SWT.DOUBLE_BUFFERED);
//		canvas.setSize(canvas.computeSize(SWT.DEFAULT, SWT.DEFAULT));
//		canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		final Display display = parent.getDisplay();
		canvas.addPaintListener(new PaintListener() {
			
			@Override
			public void paintControl(PaintEvent e) {
				// TODO Auto-generated method stub
//				Image image = new Image(display, canvas.getBounds().width, canvas.getBounds().height);
//				GC gc = new GC(image);
//				gc.setBackground(e.gc.getBackground());
//				gc.setForeground(e.gc.getForeground());
				e.gc.setBackground(new Color(null, 255, 255, 255));
				e.gc.fillRectangle(0, 0, canvas.getSize().x, canvas.getSize().y);
				onDraw(e.gc);
//				canvas.setSize(canvas.computeSize(SWT.DEFAULT, SWT.DEFAULT));
//				canvas.layout();
//				e.gc.drawImage(image, 0, 0);
//				gc.dispose();
//				image.dispose();
			}
		});
		
		
		addKeyListener();
		addMouseListener();
		
		composite.setContent(canvas);
//		composite.setMinSize(canvas.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
//		start();
		
	}

	private void executeKeyEvent(int state, KeyEvent event){
		PageViewInterface pageView = pageManager.peak();
		if(pageView != null){
			pageView.onKey(state, event);
		}
	}
	
	private void executeMouseEvent(int state, MouseEvent event){
		PageViewInterface pageView = pageManager.peak();
		if(pageView != null){
			pageView.onMouseEvent(state, event);
		}
	}
	
	private void addKeyListener(){
		canvas.addKeyListener(new KeyListener() {
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				executeKeyEvent(DrawableInterface.EVENT_STATE_UP, e);
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				executeKeyEvent(DrawableInterface.EVENT_STATE_DOWN, e);
			}
		});
	}
	
	/**
	 * 得到页面UI管理器
	 * @return
	 */
	public PageManager getPageManager(){
		return pageManager;
	}
	
	private void addMouseListener(){
		canvas.addMouseMoveListener(new MouseMoveListener() {
			
			@Override
			public void mouseMove(MouseEvent e) {
				// TODO Auto-generated method stub
				executeMouseEvent(DrawableInterface.EVENT_STATE_MOVE, e);
			}
		});
		
		canvas.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseUp(MouseEvent e) {
				// TODO Auto-generated method stub
				executeMouseEvent(DrawableInterface.EVENT_STATE_UP, e);
			}
			
			@Override
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				executeMouseEvent(DrawableInterface.EVENT_STATE_DOWN, e);
			}
			
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				// TODO Auto-generated method stub
				executeMouseEvent(DrawableInterface.EVENT_STATE_DOUBLE_CLICK, e);
			}
		});
	}
	
	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		stop();
		super.dispose();
	}
	
	/**
	 * 停止绘制线程
	 */
	private void stop(){
		isAlive = false;
		pageManager.popAll();
	}
	
	/**
	 * 开始绘制
	 */
	public void start(){
		isAlive = true;
//		new Thread(this).start();
		getParent().getDisplay().asyncExec(new Thread(this));
	}
	
	/**
	 * 绘制主入口方法
	 * @param gc
	 */
	public void onDraw(GC gc){
		PageViewInterface pageView = pageManager.peak();
		if(pageView != null){
			pageView.onDraw(gc);
		}
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
	public void reDraw(){
		if(!isDisposed()){
			canvas.redraw();
		}
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(isAlive){
//			getParent().getDisplay().asyncExec(new Runnable() {
//				@Override
//				public void run() {
					// TODO Auto-generated method stub
					long startTime = System.currentTimeMillis();
					canvas.redraw();
					long useTime = System.currentTimeMillis() - startTime;
					if(useTime < delay){
						try {
							Thread.sleep(delay - useTime);
							System.out.println("@@");
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
//				}
//			});
		}
	}
}
